﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ltModel;
using System.Data;
using System.Text;
using ltModel.Model;
using ltModel.BLL;

public partial class ProjectList : System.Web.UI.Page
{
    #region Fields

    private int projectTypeID = -1;
    private int projectStreetID = -1;
    private int pageIndex = -1;
    private string allStr = "全部";
    private string singalTypeTD = "<TD><TABLE cellSpacing=2 cellPadding=2><TR><TD class=font align=middle><A class=A1 href='ProjectList.aspx?projectType={0}&projectStreet={1}'>{2}</A></TD></TR></TBODY></TABLE></TD>";
    private string singalTypeTDSelected = "<TD><TABLE cellSpacing=2 cellPadding=2><TR><TD class=font align=middle style='font-weight: bold;'><A class=A1 href='ProjectList.aspx?projectType={0}&projectStreet={1}'>{2}</A></TD></TR></TBODY></TABLE></TD>";
    private string singalStreetTD = "<TD><TABLE cellSpacing=2 cellPadding=2><TR><TD class=font align=middle><A class=A1 href='ProjectList.aspx?projectType={0}&projectStreet={1}'>{2}</A></TD></TR></TBODY></TABLE></TD>";
    private string singalStreetTDSelected = "<TD><TABLE cellSpacing=2 cellPadding=2><TR><TD class=font align=middle style='font-weight: bold;'><A class=A1 href='ProjectList.aspx?projectType={0}&projectStreet={1}'>{2}</A></TD></TR></TBODY></TABLE></TD>";
   
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                this.addBtnTD.Visible = false;
                return;
            }
        }
    }

    /// <summary>
    /// this Methos is used to judge if the project is overdue
    /// </summary>
    /// <param name="endDate"></param>
    /// <returns></returns>
    protected string JudgeProjectStatus(object endDate)
    {
        string status = "招募中";
        int year = 2012;
        int month = 1;
        int day = 1;

        string[] array = endDate.ToString().Split(new char[]{'/'});
        int.TryParse(array[0], out year);
        int.TryParse(array[1], out month);
        int.TryParse(array[2].Substring(0,2), out day);

        DateTime dueDate = new DateTime(year, month, day,0, 0, 0);
        DateTime now = DateTime.Now;

        if (dueDate >= now)
        {
            status = "招募中";
        }
        else
        {
            status = "招募结束";
        }

        return status;
    }

    /// <summary>
    /// This Method is used to control the length of the project name. css or something
    /// </summary>
    /// <param name="str"></param>
    /// <param name="len"></param>
    /// <returns></returns>
    protected string LeftString(object str, int len)
    {
        return Str.LeftString(str.ToString(), len);
    }

    #region Private Methods

    private void BindData()
    {
        if (HttpContext.Current.Request.QueryString["projectType"] != null)
        {
            int.TryParse(HttpContext.Current.Request.QueryString["projectType"], out projectTypeID);
        }
        if (HttpContext.Current.Request.QueryString["projectStreet"] != null)
        {
            int.TryParse(HttpContext.Current.Request.QueryString["projectStreet"], out projectStreetID);
        }
        if (HttpContext.Current.Request.QueryString["pageindex"] != null)
        {
            int.TryParse(HttpContext.Current.Request.QueryString["pageindex"], out pageIndex);
        }


        BindProjectTypeList();
        BindProjectStreetList();
        BindProjectList();
    }
    
    /// <summary>
    /// This method is used to get the the project list by the parameters
    /// </summary>
    private void BindProjectList()
    {
        StringBuilder strWhere = new StringBuilder();
        if (this.projectStreetID > 0)
        {
            strWhere.Append(" and (lt_ProjectStreet.streetID = " + this.projectStreetID+")");
        }
        if (this.projectTypeID > 0)
        {
            strWhere.Append(" and (lt_ProjectType.typeID = " + this.projectTypeID + ")");
        }

        ltModel.PageFen p = new ltModel.PageFen();
        p.Table = "lt_Project,lt_ProjectStreet,lt_ProjectType";
        p.Fields = "* ";
        p.PageIndex = 1;
        p.PageSize = 15;
        p.OrderBy = "RegistrationDeadLine desc , ProjectID desc ";
        p.PkName = "ProjectID";
        p.Where = " ((lt_Project.projectStreetID = lt_ProjectStreet.streetID) and (lt_Project.projectTypeID = lt_ProjectType.typeID) and (lt_Project.approved = 1) " + strWhere.ToString() + " )";
        DataTable dt = p.GetListForProject(projectTypeID,projectStreetID);

        if (dt.Rows.Count > 0)
        {
            Repeater1.Visible = true;
            Literal1.Text = p.Link;
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
        else
        {
            Repeater1.Visible = false;

            if (pageIndex == -1)
            {
                this.dataListTable.Height = "0";
                Literal1.Text = "<p align='center' style='font-size:13px;'>没有记录!</p>";
            }
            else
            {
                Literal1.Text = "<p align='center' style='font-size:13px;'>本页没有记录!</p>";
            }

        }
        dt.Dispose();
    }

    /// <summary>
    /// This method is used to get the projecttype list and buid the html for this part
    /// </summary>
    private void BindProjectTypeList()
    {
        ProjectTypeBLL typeBLL = new ProjectTypeBLL();
        List<ProjectType> projectTypeList = typeBLL.GetList();
        StringBuilder content = new StringBuilder();

        //Build the html structure for the ProjectTypeList
        if (projectTypeList != null && projectTypeList.Count > 0)
        {
            int typeCountInRow = 11;
            int totalTypeCount = projectTypeList.Count;
            int rows =Convert.ToInt32( Math.Ceiling((totalTypeCount+1)/ (typeCountInRow*1.0)));

            this.ltlProjectType.Text = rows.ToString();

            //Build title
            content.Append("<tr><td class='blue'><b>项目类别</b></td></tr>");
            //Build Content
            content.Append("<tr><td align='center'><!--项目类别搜索--><TABLE style='WIDTH: 95%; BORDER-COLLAPSE: collapse' id=Sort_List border=0 cellSpacing=0>");


            for (int i = 0; i < rows; i++)
            {
                content.Append("<tr>");

                for (int j = 0; j < typeCountInRow; j++)
                {
                    int indexOfProjectType = i*typeCountInRow +j;
                    if (indexOfProjectType < totalTypeCount)
                    {
                        if (projectTypeID == projectTypeList[indexOfProjectType].TypeID)
                        {
                            content.AppendFormat(singalTypeTDSelected, projectTypeList[indexOfProjectType].TypeID, projectStreetID, projectTypeList[indexOfProjectType].TypeName);
                        }
                        else
                        {
                            content.AppendFormat(singalTypeTD, projectTypeList[indexOfProjectType].TypeID, projectStreetID, projectTypeList[indexOfProjectType].TypeName);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                //Build All html
                if (i == (rows - 1))
                {
                    if (projectTypeID == -1)
                    {
                        content.AppendFormat(singalTypeTDSelected, "-1", projectStreetID, allStr);
                    }
                    else
                    {
                        content.AppendFormat(singalTypeTD, "-1", projectStreetID, allStr);
                    }
                }

                content.Append("</tr>");
            }

            content.Append("</table></td></tr>");           
        }

        this.ltlProjectType.Text = content.ToString();
    }

    /// <summary>
    /// This Method is used to bind the ProjectStreetList
    /// </summary>
    private void BindProjectStreetList()
    {
        ProjectStreetBLL streetBLL = new ProjectStreetBLL();
        List<ProjectStreet> projectStreetList = streetBLL.GetList();
        StringBuilder content = new StringBuilder();

        //Build the html structure for the ProjectTypeList
        if (projectStreetList != null && projectStreetList.Count > 0)
        {
            int typeCountInRow = 10;
            int totalStreetCount = projectStreetList.Count;
            int rows = Convert.ToInt32(Math.Ceiling((totalStreetCount + 1) / typeCountInRow*1.0));

            //Build title
            content.Append("<tr><td class='blue'><b>乡镇街道</b></td></tr>");
            //Build Content
            content.Append("<tr><TD align=middle><!--项目所在区县搜索--><TABLE style='WIDTH: 95%; BORDER-COLLAPSE: collapse' id=QXList border=0 cellSpacing=0>");

            for (int i = 0; i < rows; i++)
            {
                content.Append("<tr>");

                for (int j = 0; j < typeCountInRow; j++)
                {
                    int indexOfProjectStreet = i * typeCountInRow + j;
                    if (indexOfProjectStreet < totalStreetCount)
                    {
                        if (projectStreetID == projectStreetList[indexOfProjectStreet].StreetID)
                        {
                            content.AppendFormat(singalStreetTDSelected, projectTypeID, projectStreetList[indexOfProjectStreet].StreetID, projectStreetList[indexOfProjectStreet].StreetName);
                        }
                        else
                        {
                            content.AppendFormat(singalStreetTD, projectTypeID, projectStreetList[indexOfProjectStreet].StreetID, projectStreetList[indexOfProjectStreet].StreetName);
                        }
                    }
                    else
                    {
                        break;
                    }
                }


                //Build All html
                if (i == (rows - 1))
                {
                    if (projectStreetID == -1)
                    {
                        content.AppendFormat(singalTypeTDSelected, projectTypeID , "-1", allStr);
                    }
                    else
                    {
                        content.AppendFormat(singalTypeTD, projectTypeID, "-1", allStr);
                    }
                }

                content.Append("</tr>");
            }

            content.Append("</table></td></tr>");
        }

        this.ltlProjectStreet.Text = content.ToString();

    }

    #endregion
}